package com.taiyi.crm.controller;

import com.taiyi.crm.pojo.entity.Permission;
import com.taiyi.crm.pojo.entity.User;
import com.taiyi.crm.service.IPermissionService;
import com.taiyi.crm.service.IUserService;
import com.taiyi.crm.util.JSONResult;
import com.taiyi.crm.util.LayUITableResult;
import com.taiyi.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@ApiIgnore
@RequestMapping("/user")
@Controller
// @RestController = @Controller + 所有方法@ResponseBody
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/getUserPage")

    public String getUserPage() {
        System.out.println("UserController.getUserPage");
        return "/user/user_list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayUITableResult list(Integer page, Integer limit, User user, Date time) {
        System.out.println(time);
        return userService.selectByPage(page, limit, user);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        userService.deleteById(id);
        return JSONResult.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(String ids) {
        userService.deleteAll(ids);
        return  JSONResult.ok("删除成功");
    }

    @RequestMapping("/getInsertPage")
    public String getInsertPage() {
        return "/user/user_insert";
    }

    @PostMapping("/insert")
    @ResponseBody
    public JSONResult insert( User user, BindingResult result, String roles) {
        System.out.println(user);
        System.out.println(roles);

        userService.insert(user, roles);
        return JSONResult.ok("添加成功");
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(User user, String roles) {
        System.out.println(user);
        System.out.println(roles);
        userService.update(user, roles);
        return JSONResult.ok("更新成功");
    }

    @RequestMapping("/getUpdatePage")
    public String getUpdatePage() {
        return "/user/user_update";
    }

    // @RequestMapping(value = "/selectUserAndRoles", method = RequestMethod.GET)
    @RequestMapping("/selectUserAndRoles")
    @ResponseBody
    public JSONResult selectUserAndRoles(Integer userId) {
        Map<String, Object> map = userService.selectUserAndRoles(userId);
        return JSONResult.ok(map);
    }

    // 如果不加method = RequestMethod.POST，这个方法默认所有的请求GET/POST/PUT/DELETE都可以处理
    // 如果限定指定处理POST，而请求的是GET请求，就会报下面的错误
    // There was an unexpected error (type=Method Not Allowed, status=405).
    // Request method 'GET' not supported

    // @RequestMapping(value = "/login", method = RequestMethod.POST)
    @PostMapping("/login")
    @ResponseBody
    public JSONResult login(String name, String password, HttpSession session) {
        UserContext.session = session;
        User user = userService.login(name, password, session);
        if (user != null) {
            // 把当前用户放到session中
            session.setAttribute(UserContext.USER_IN_SESSION, user);
            // 查询该用户拥有的权限,并放到session中
            List<Permission> permissions = permissionService.selectByUserId(user.getId());
            session.setAttribute(UserContext.PERMISSION_IN_SESSION, permissions);
            return JSONResult.ok("登录成功");
        }
        return JSONResult.error("登录失败");
    }

    @RequestMapping("/getLoginPage")
    public String getLoginPage() {
        System.out.println("UserController.getLoginPage");
        return "/user/login";
    }

    @RequestMapping("/getRegisterPage")
    public String getRegisterPage() {
        System.out.println("UserController.getRegisterPage");
        return "/user/register";
    }

    @RequestMapping("register")
    @ResponseBody
    public JSONResult register(User user) {
        System.out.println("UserController.register");
        userService.insert_register(user);
        return JSONResult.ok("注册成功");
    }


}

